<?php

namespace App\Remote\XiaomiPush;

use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class XiaomiPushRemote extends Remote
{
    /**
     * 根据regId发送消息
     *
     * @param string $regId
     * @param string $title
     * @param string $desc
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function regIdPush(string $regId, string $title, string $desc)
    {
        return self::customRequestV3(
            "/v2/message/regid",
            RequestAlias::METHOD_POST,
            [
                "title"               => $title,
                "description"         => $desc,
                "registration_id"     => $regId,
                "notify_id"           => time(),
                "extra.kf"            => json_encode(["name" => "asdfasf"], JSON_UNESCAPED_UNICODE),
                "extra.notify_effect" => 1
            ]
        );
    }


    /**
     * @param array $regIds
     * @param string $title
     * @param string $desc
     * @param string $path
     * @param array $extra
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function regIdsMultiPush(array $regIds, string $title, string $desc, string $path, array $extra)
    {
        return self::customRequestV3(
            "/v2/multi_messages/regids",
            RequestAlias::METHOD_POST,
            [
                "messages" => collect($regIds)->map(function ($regId) use ($title, $desc, $path, $extra) {
                    return [
                        "target"  => $regId,
                        "message" => [
                            "title"       => mb_substr(str_replace("\"", "", $title), 0, 50),
                            "description" => mb_substr(str_replace("\"", "", $desc), 0, 100),
                            "notify_id"   => time(),
                            "extra"       => [
                                "kf" => array_merge([
                                    "path" => $path
                                ], $extra)
                            ],
                        ]
                    ];
                })->toJson(JSON_UNESCAPED_UNICODE)
            ]
        );
    }
}
